﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Rostfritt.Core;
using Rostfritt.Core.Entities;
using System.Data;

namespace Rostfritt.Web.Controllers
{
    public class EmployerController : Controller
    {
        //
        // GET: /Employer/

        RostfrittContext db = new RostfrittContext();

        public ActionResult Index()
        {
            return View(db.Employers.ToList());
        }

        //
        // GET: /Employer/Details/5

        public ActionResult Details(int id)
        {
            var employer = db.Employers.Find(id);

            return View(employer);
        }

        //
        // GET: /Employer/Create

        public ActionResult Create()
        {   
            return View(new Employer());
        }

        //
        // POST: /Employer/Create

        [HttpPost]
        public ActionResult Create(Employer employer)
        {
            try
            {
                db.Employers.Add(employer);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(employer);
            }
        }

        //
        // GET: /Employer/Edit/5

        public ActionResult Edit(int id)
        {
            var employer = db.Employers.Find(id);

            if (employer == null)
                return HttpNotFound();

            return View(employer);
        }

        //
        // POST: /Employer/Edit/5

        [HttpPost]
        public ActionResult Edit(Employer employer)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employer).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(employer);
        }

        //
        // GET: /Employer/Delete/5

        public ActionResult Delete(int id)
        {
            var employer = db.Employers.Find(id);

            if (employer == null)
                return HttpNotFound();

            return View(employer);
        }

        //
        // POST: /Employer/Delete/5

        [HttpPost]
        public ActionResult Delete(Employer employer)
        {
            try
            {
                db.Employers.Remove(employer);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(employer);
            }
        }
    }
}
